package com.litengsoft.lianmeng.service.impl;

import com.litengsoft.lianmeng.common.HttpUtils;
import com.litengsoft.lianmeng.service.LocalCacheService;
import com.litengsoft.lianmeng.service.WXService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service("wxService")
public class WXServiceImpl implements WXService {

    @Value("${web_wx_init_url}")
    private String initUrl;

    @Value("${web_wx_contact_url}")
    private String contactUrl;

    @Resource(name = "localCacheService")
    private LocalCacheService localCacheService;

    /**
     * 初始化微信
     */
    @Override
    public String wxInit() {
        String url = String.format(initUrl, System.currentTimeMillis());
        String uin = localCacheService.get("wxuin").toString();
        String sid = localCacheService.get("wxsid").toString();
        Map<String, String> uinSidMap = new HashMap();
        uinSidMap.put("Uin", uin);
        uinSidMap.put("Sid", sid);
        uinSidMap.put("DeviceID", "e1615250492");
        Map map = new HashMap();
        map.put("BaseRequest", uinSidMap);
        try {
            String initResult = HttpUtils.sendHttpPost(url, map);
            log.info("初始化微信返回结果：{}", initResult);
            return initResult;
        } catch (Exception e) {
            log.error("初始化微信失败,uin:{},sid:{}", uin, sid, e);
        }
        return null;
    }

    /**
     * 获取用户好友列表
     *
     * @return
     */
    @Override
    public String getMemberList() {
        String url = String.format(contactUrl, System.currentTimeMillis());
        try {
            String initResult = HttpUtils.sendHttpPost(url);
            log.info("获取好友列表返回结果：{}", initResult);
            return initResult;
        } catch (Exception e) {
            log.error("获取好友列表返回结果失败,uin:{},sid:{}", e);
        }
        return null;
    }
}
